/////////////////////////////////////////////////////////////////////////////
// Name:        PropertiesNotebook.cpp
// Purpose:     
// Author:      yospaly
// Modified by: 
// Created:     24/12/2008 16:59:25
// RCS-ID:      
// Copyright:   Copyright (C) 2008, yospaly
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
#include "wx/imaglist.h"
////@end includes

#include "PropertiesNotebook.h"
#include "events.h"
#include "inibuilder.h"

////@begin XPM images
////@end XPM images


/*!
 * PropertiesNotebook type definition
 */

IMPLEMENT_DYNAMIC_CLASS( PropertiesNotebook, wxAuiNotebook )


/*!
 * PropertiesNotebook event table definition
 */

BEGIN_EVENT_TABLE( PropertiesNotebook, wxAuiNotebook )

////@begin PropertiesNotebook event table entries
    EVT_AUINOTEBOOK_PAGE_CHANGED( ID_PROPERTIES_NOTEBOOK, PropertiesNotebook::OnPageChanged )

////@end PropertiesNotebook event table entries

	EVT_COMMAND(ID_PROPERTIES_NOTEBOOK, wxEVT_PKG_PROPERTIES_EVENT, PropertiesNotebook::OnPkgProperties)

END_EVENT_TABLE()


/*!
 * PropertiesNotebook constructors
 */

PropertiesNotebook::PropertiesNotebook()
{
    Init();
}

PropertiesNotebook::PropertiesNotebook(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style)
{
    Init();
    Create(parent, id, pos, size, style);
}


/*!
 * PropertiesNotebook creator
 */

bool PropertiesNotebook::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style)
{
////@begin PropertiesNotebook creation
    wxAuiNotebook::Create(parent, id, pos, size, style);
    CreateControls();
////@end PropertiesNotebook creation
    return true;
}


/*!
 * PropertiesNotebook destructor
 */

PropertiesNotebook::~PropertiesNotebook()
{
////@begin PropertiesNotebook destruction
////@end PropertiesNotebook destruction
	
    EventCenter::unregister(wxEVT_PKG_PROPERTIES_EVENT, this);
}


/*!
 * Member initialisation
 */

void PropertiesNotebook::Init()
{
////@begin PropertiesNotebook member initialisation
    p_desc_htmlwnd = NULL;
    p_depend_htmlwnd = NULL;
    p_files_htmlwnd = NULL;
    p_detail_htmlwnd = NULL;
    p_citruslog_textctrl = NULL;
////@end PropertiesNotebook member initialisation
}


/*!
 * Control creation for PropertiesNotebook
 */

void PropertiesNotebook::CreateControls()
{    
////@begin PropertiesNotebook content construction
    PropertiesNotebook* itemAuiNotebook1 = this;

    p_desc_htmlwnd = new wxHtmlWindow( itemAuiNotebook1, ID_DESC_HTMLWND, wxDefaultPosition, wxSize(200, 150), wxHW_SCROLLBAR_AUTO|wxNO_BORDER|wxHSCROLL|wxVSCROLL );

    this->AddPage(p_desc_htmlwnd, _("Description"), false);

    p_depend_htmlwnd = new wxHtmlWindow( itemAuiNotebook1, ID_DEPEND_HTMLWND, wxDefaultPosition, wxSize(200, 150), wxHW_SCROLLBAR_AUTO|wxNO_BORDER|wxHSCROLL|wxVSCROLL );

    this->AddPage(p_depend_htmlwnd, _("Dependencies"), false);

    p_files_htmlwnd = new wxHtmlWindow( itemAuiNotebook1, ID_HTMLWINDOW, wxDefaultPosition, wxSize(200, 150), wxHW_SCROLLBAR_AUTO|wxNO_BORDER|wxHSCROLL|wxVSCROLL );

    this->AddPage(p_files_htmlwnd, _("Installed Files"), false);

    p_detail_htmlwnd = new wxHtmlWindow( itemAuiNotebook1, ID_DETAIL_HTMLWND, wxDefaultPosition, wxSize(200, 150), wxHW_SCROLLBAR_AUTO|wxNO_BORDER|wxHSCROLL|wxVSCROLL );

    this->AddPage(p_detail_htmlwnd, _("Details"), false);

    p_citruslog_textctrl = new wxTextCtrl( itemAuiNotebook1, ID_CITRUSLOG_TEXTCTRL, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxNO_BORDER );

    this->AddPage(p_citruslog_textctrl, _("Citrus Log"), false);

////@end PropertiesNotebook content construction
    
    EventCenter::registerr(wxEVT_PKG_PROPERTIES_EVENT, this);

    wxLog::SetLogLevel(wxLOG_Info);
    wxLog* logger = new wxLogTextCtrl(p_citruslog_textctrl);
    wxLog::SetActiveTarget(logger);
}


void PropertiesNotebook::OnPkgProperties(wxCommandEvent &event) {
	IniBuilder& inibuilder = IniBuilder::get();
	
	PkgInfo& pkg_info = inibuilder.packages[event.GetString()];
	
	// descriptions
	wxString html = wxString::Format(_T("<h5>%s</h5>%s"),
		pkg_info.sdesc.c_str(),
		pkg_info.ldesc.c_str()
	);
	
	this->p_desc_htmlwnd->SetPage(html);
	
	
	// dependencies
	
	html = _T("<p><b><font size=\"+1\">This package depends on the following packages</font></b></p>"
			  "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">");
	
	if (pkg_info.require.GetCount()<=0) {
		html.Append(_T("<tr><td>None</td></tr>"));
	} else {
		for (int i=0; i<pkg_info.require.GetCount(); i++) {
			if (0==i%4) {
				html.Append(_T("<tr>"));
			}
			
			html.Append(_T("<td>"));
			html.Append(pkg_info.require[i]);
			html.Append(_T("</td>"));
	
			if (0==i%4) {
				html.Append(_T("</tr>"));
			}
		}
	}
	
	html.Append(_T("</table>"));
	
	html.Append(_T("<p><b><font size=\"+1\">The following packages depends on the this packages</font></b></p>"
			  "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">"));
	
	if (pkg_info.dependent.GetCount()<=0) {
		html.Append(_T("<tr><td>None</td></tr>"));
	} else {
		for (int i=0; i<pkg_info.dependent.GetCount(); i++) {
			if (0==i%4) {
				html.Append(_T("<tr>"));
			}
			
			html.Append(_T("<td>"));
			html.Append(pkg_info.dependent[i]);
			html.Append(_T("</td>"));
	
			if (0==i%4) {
				html.Append(_T("</tr>"));
			}
		}
	}
	
	html.Append(_T("</table>"));
	
	this->p_depend_htmlwnd->SetPage(html);
	
	//===== installed files
	wxArrayString files = inibuilder.get_installed_files(pkg_info.name);

	html = _T("");
	
	for ( int i=0; i<files.GetCount(); i++ ) {
		html.Append(_T("<tt>"));
		html.Append(files[i]);
		html.Append(_T("</tt> <br>"));
	}
	
	p_files_htmlwnd->SetPage(html);
	
	// 
	/*
	"<b>Install:</b> %s<br>"
	"<b>MD5:</b> %s<br>"
	"<b>Source:</b> %s<br>"
	"<b>Source MD5:</b> %s"
	pkg_info.sdesc.c_str(),
	pkg_info.install_path.c_str(),
	pkg_info.install_size.c_str(),
	pkg_info.source_path.c_str(),
	pkg_info.source_size.c_str(),*/
}

/*!
 * Should we show tooltips?
 */

bool PropertiesNotebook::ShowToolTips()
{
    return true;
}

/*!
 * Get bitmap resources
 */

wxBitmap PropertiesNotebook::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin PropertiesNotebook bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end PropertiesNotebook bitmap retrieval
}

/*!
 * Get icon resources
 */

wxIcon PropertiesNotebook::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin PropertiesNotebook icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end PropertiesNotebook icon retrieval
}


/*!
 * wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGED event handler for ID_PROPERTIES_NOTEBOOK
 */

void PropertiesNotebook::OnPageChanged( wxAuiNotebookEvent& event )
{
////@begin wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGED event handler for ID_PROPERTIES_NOTEBOOK in PropertiesNotebook.
    // Before editing this code, remove the block markers.
    event.Skip();
////@end wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGED event handler for ID_PROPERTIES_NOTEBOOK in PropertiesNotebook.
}

